/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.prod;


import dp1.titandevelop.titano.persistent.Asignacion;
import dp1.titandevelop.titano.persistent.Distribuidor;
import dp1.titandevelop.titano.service.AsignacionService;
import dp1.titandevelop.titano.service.ConexionService;
import dp1.titandevelop.titano.service.DistribuidorService;
import dp1.titandevelop.titano.service.ValidacionService;
import java.awt.Cursor;
import java.awt.FileDialog;
import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Yulian
 */
public class ModulosT_PRO_ReporteAsiganacionUnica extends javax.swing.JInternalFrame {

    //private List<Asignacion> listaAsignacion;
    private List<Integer> listaIdAsignacion;
    //private List<Integer> listaId;
    public Integer idAsignacion;
    private void addList (){
        
        for(int i=0;i<listaIdAsignacion.size();i++){
            this.cboxAlmacen.addItem(listaIdAsignacion.get(i));
        }
    }
    public ModulosT_PRO_ReporteAsiganacionUnica() {
        initComponents();
        
        AsignacionService servAsig = new AsignacionService();
        //this.listaIdAsignacion = servAsig.buscarTodo();
        this.listaIdAsignacion=servAsig.ObtenerIdAsignaciones();
        addList();
    }
    public int cboxIndex(JComboBox cbox){
        return cbox.getSelectedIndex()-1;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnReporte = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        cboxAlmacen = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jTextDescripcion = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setResizable(true);
        setTitle("Reporte de detalle de Asignación");

        btnReporte.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/reporte2.png"))); // NOI18N
        btnReporte.setToolTipText("Click para generar reporte");
        btnReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReporteActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Parametros"));

        cboxAlmacen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar" }));
        cboxAlmacen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboxAlmacenActionPerformed(evt);
            }
        });

        jLabel1.setText("Codigo Asignacion");

        jTextDescripcion.setEditable(false);

        jLabel2.setText("Descripción:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(cboxAlmacen, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(57, 57, 57)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboxAlmacen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jTextDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(16, 16, 16))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnReporte)
                .addGap(0, 0, 0)
                .addComponent(btnCancelar)
                .addGap(10, 10, 10))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnReporte, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(8, 8, 8))
        );

        jPanel2.getAccessibleContext().setAccessibleName("Rango");
        jPanel2.getAccessibleContext().setAccessibleDescription("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReporteActionPerformed
        
        ValidacionService val = new ValidacionService();
        
        if(!val.esCBoxVacio(cboxAlmacen)){
             
            AsignacionService servAsig = new AsignacionService();
            List<Asignacion> t=servAsig.buscarIdAsignacion((this.listaIdAsignacion.get(this.cboxAlmacen.getSelectedIndex()-1)));
            
            List<Asignacion> manhana=new  ArrayList<Asignacion>();
            List<Asignacion> tarde=new  ArrayList<Asignacion>();
            List<Asignacion> noche=new  ArrayList<Asignacion>();
            for(int i=0;i<t.size();i++){
            if(t.get(i).getToTurno().getIdturno().equals(1)){
            manhana.add(t.get(i));
            }
            if(t.get(i).getToTurno().getIdturno().equals(2)){
            tarde.add(t.get(i));
            }
            if(t.get(i).getToTurno().getIdturno().equals(3)){
            noche.add(t.get(i));
            }
            }
            
            try{
                
                String nombreArchivo = "/tmp/reporte-asignacion.pdf";
            FileDialog fd = new FileDialog(new JFrame(), "Guardar", FileDialog.SAVE);
            fd.setDirectory(".");
            fd.setFile("*.pdf");
            fd.setVisible(true);
            nombreArchivo = fd.getDirectory() + fd.getFile();
            
                
                if(fd.getFile()!=null){
                    this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));  
                    printAsignacion print2=new printAsignacion(manhana,tarde,noche,nombreArchivo);
                    this.setCursor(Cursor.getDefaultCursor());
                    JOptionPane.showInternalMessageDialog(this.getRootPane(),"El Reporte a sido generado satisfactoriamente","Mensaje",JOptionPane.INFORMATION_MESSAGE);
                }
                
            }catch(Exception e){
                
                e.printStackTrace();
             System.out.println("mensaje de Error:"+ e.getMessage());
            }
        }
        else{
            JOptionPane.showInternalMessageDialog(this.getRootPane(), "Debe seleccionar una asignación","Aviso",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnReporteActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void cboxAlmacenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboxAlmacenActionPerformed
        int select = cboxAlmacen.getSelectedIndex()-1;
        AsignacionService service= new AsignacionService();

        //jTextField1.setText("");
        //jTextField1.setText(service.ObtenerDescripcion(this.listaIdAsignacion.get(select)));

        jTextDescripcion.setText(service.ObtenerDescripcion(this.listaIdAsignacion.get(select)));
        this.jTextDescripcion.setEnabled(false);

    }//GEN-LAST:event_cboxAlmacenActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnReporte;
    private javax.swing.JComboBox cboxAlmacen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextDescripcion;
    // End of variables declaration//GEN-END:variables
}
